package me.muci.A_base.processingFlow;

import jdk.nashorn.internal.ir.CallNode;
import org.junit.Test;

import java.io.*;

/**
 * ClassName: InputStreamReaderTest
 * <p>
 * Description: 将字节输入流转换成字符的转换流
 * <p>
 * date: 2021/8/13 16:02
 *
 * @author muci
 * @since JDK 1.8
 */
public class InputStreamReaderOutputStreamWriterTest {
    @Test
    public void inputStreamReaderTest() {

        //字符集的使用是根据 文件保存时的字符集
        try (FileInputStream fis = new FileInputStream("src\\main\\resources\\inputStreamReader\\file.txt");
             InputStreamReader inputStreamReader = new InputStreamReader(fis, "UTF-8");) {
            char[] chars = new char[50];
            int len;
            while ((len = inputStreamReader.read(chars)) != -1) {
                String string = new String(chars, 0, len);
                System.out.println(string);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Test
    public void inputStreamReaderAndOutputStreamWriter() {
        try (
                FileInputStream fis = new FileInputStream("src\\main\\resources\\inputStreamReader\\file.txt");
                FileOutputStream fos = new FileOutputStream("src\\main\\resources\\inputStreamReader\\file_copy.txt");
                InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
                OutputStreamWriter osw = new OutputStreamWriter(fos, "gbk");
        ) {
            char[] chars = new char[50];
            int len;
            while ((len = isr.read(chars)) != -1) {
                osw.write(chars, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
